%%%-------------------------------------------------------------------
%% @doc himq top level supervisor.
%% @end
%%%-------------------------------------------------------------------

-module(himq_sup).

-behaviour(supervisor).

-export([start_link/0]).

-export([init/1]).
-include("himq.hrl").
-define(SERVER, ?MODULE).

start_link() ->
  supervisor:start_link({local, ?SERVER}, ?MODULE, []).

%% sup_flags() = #{strategy => strategy(),         % optional
%%                 intensity => non_neg_integer(), % optional
%%                 period => pos_integer()}        % optional
%% child_spec() = #{id => child_id(),       % mandatory
%%                  start => mfargs(),      % mandatory
%%                  restart => restart(),   % optional
%%                  shutdown => shutdown(), % optional
%%                  type => worker(),       % optional
%%                  modules => modules()}   % optional
init([]) ->
  SupFlags = #{strategy => one_for_one, %%重启策略
    intensity => 10,%%时间周期内的重启次数
    period => 10},%% 时间周期

  PoolOptions = [{size, 10}, {max_overflow, 100}],
  MySqlOptions = [{user, "root"}, {password, "123456"}, {database, "himq"}],
  %%lager:info("pool ~p",[himq_mysql:child_spec(?HIMQ, PoolOptions, MySqlOptions)]),
  ChildSpecs = [
    himq_mysql:child_spec(?HIMQ, PoolOptions, MySqlOptions),
    #{
      id=>himq_server,
      start=>{gen_server2, start_link, [himq_server, [], []]},
      type=>worker,
      restart=>permanent,
      shutdown=>brutal_kill
    },
    #{
      id=>himq_session,
      start=>{gen_server2, start_link, [himq_session, [], []]},
      type=>worker,
      restart=>permanent,
      shutdown=>brutal_kill
    }
  ],
  {ok, {SupFlags, ChildSpecs}}.

